import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';
import 'package:xindong/handle/xindong_user_handle.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/xindong_common_switch_item.dart';

class XinDongSettingPrivacyYeMian extends StatefulWidget {
  const XinDongSettingPrivacyYeMian({Key? key}) : super(key: key);

  @override
  State<XinDongSettingPrivacyYeMian> createState() => _XinDongSettingPrivacyYeMianState();
}

class _XinDongSettingPrivacyYeMianState extends State<XinDongSettingPrivacyYeMian> {
  final _userHandle = XinDongUserHandle();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      // 拉取用户信息
      XinDongUserController.to.requestConfig();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XinDongAppBar("隐私设置"),
      body: GetBuilder<XinDongUserController>(builder: (controller) {
        return ListView(
          children: [
            XinDongCommonSwitchItem(
              title: "对他人隐藏我的距离",
              value: controller.userInfo?.configPersonal?.hide_distance == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "hide_distance", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.hide_distance = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "隐藏自己的守护天使",
              value: controller.userInfo?.configPersonal?.hide_defend_angel == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "hide_defend_angel", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.hide_defend_angel = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "隐藏自己的守护者",
              value: controller.userInfo?.configPersonal?.hide_defends == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "hide_defends", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.hide_defends = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "隐藏自己的土豪值、魅力值",
              value: controller.userInfo?.configPersonal?.hide_charm == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "hide_charm", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.hide_charm = value;
                  controller.update();
                }
              },
            ),
            XinDongCommonSwitchItem(
              title: "隐藏自己的勋章和礼物",
              value: controller.userInfo?.configPersonal?.hide_gift == 1,
              onChanged: (open) async {
                final value = open ? 1 : 0;
                final result = await _userHandle.userConfigEdit(key: "hide_gift", value: value);
                if (result) {
                  controller.userInfo?.configPersonal?.hide_gift = value;
                  controller.update();
                }
              },
            ),
          ],
        );
      }),
    );
  }
}
